#!/usr/bin/env python

#import public lib
import string

#import private lib
import ConfigClass
import CommandMaker
import ConnFactory
import validator
from CommonLib import log

def action(profile_id, action_type):
    
    profile= ConfigClass.Profile(profile_id)
    
    section = profile.get_section()
    
    if section == 'WAS':
        
        was_action(profile, action_type)

    elif section == 'TOMCAT':
        
        tomcat_action(profile, action_type)
    
    elif section == 'APACHE':
        
        apache_action(profile, action_type)
    
    elif section == 'DB2':
        
        db2_action(profile, action_type)
    
    elif section == 'AIX':
        
        aix_action(profile, action_type)
    
    elif section == 'SUIT':
        
        suit_action(profile)
    
    else:
        
        print 'the section type: ' + section + ' in the profile file is unknown '
        
        
def tomcat_action(profile, action_type):

    host_config = {}
    host_config['id'] = profile.get_id()
    host_config['section']= profile.get_section()
    host_config['host']   = profile.get_host()
    host_config['system'] = profile.get_system()
    host_config['user']   = profile.get_user()
    host_config['passwd'] = profile.get_passwd() 
    host_config['home']   = profile.get_home()
    host_config['comm']   = profile.get_comm()
    host_config['download'] = profile.get_download()
    host_config['duser'] = profile.get_duser()
    host_config['dpasswd'] = profile.get_dpasswd()
    host_config['ftpport'] = profile.get_ftpport()
        
    if action_type == 'START':
        
        if  host_config['comm'] == 'TELNET':
            
            command_list =  []
            
            checkpoint = 'Using CLASSPATH'
            
            CommandMaker.make_start_command(host_config, command_list)
            
            ConnFactory.conn_tel(host_config, action_type, command_list, checkpoint)
        
        else:
            
            print 'The communicate way: ' + host_config['comm'] + ' is unknown.'
    
    elif action_type == 'STOP':
        
        if  host_config['comm'] == 'TELNET':
            
            command_list =  []
            
            checkpoint = 'Using CLASSPATH'
            
            CommandMaker.make_stop_command(host_config, command_list)
            
            msg = ConnFactory.conn_tel(host_config, action_type, command_list, checkpoint)
            log(msg)
        else:
            
            print 'The communicate way: ' + host_config['comm'] + ' is unknown.'
    
    elif action_type == 'GLOG':
        
        if host_config['download'] == 'FTP':
            
            command_list = []
            
            #CommandMaker.make_ftp_command(host_config, command_list)
            ConnFactory.conn_ftp(host_config, action_type, command_list)
        
        else:
            
            print 'The download way: ' + host_config['download'] + ' is unknown.'

    elif action_type == 'DLOG':
        
        if  host_config['comm'] == 'TELNET':
            
            command_list =  []
            
            checkpoint = ''
                     
            CommandMaker.make_command(host_config, action_type, command_list)
            
            ConnFactory.conn_tel(host_config, action_type, command_list, checkpoint)
        
        else:
            
            print 'The communicate way: ' + host_config['comm'] + ' is unknown.'
    
    elif action_type == 'DTEMP':
        
        if  host_config['comm'] == 'TELNET':
            
            command_list =  []
            
            checkpoint = ''
                     
            CommandMaker.make_command(host_config, action_type, command_list)
            
            ConnFactory.conn_tel(host_config, action_type, command_list, checkpoint)
        
        else:
            
            print 'The communicate way: ' + host_config['comm'] + ' is unknown.'
        
    
    else:
        
        print 'the action type: ' + action_type + ' is unknown.'


def was_action(profile, action_type):

    host_config = {}
    host_config['id'] = profile.get_id()
    host_config['section']= profile.get_section()
    host_config['type']   = profile.get_type()
    host_config['host']   = profile.get_host()
    host_config['system'] = profile.get_system()
    host_config['user']   = profile.get_user()
    host_config['passwd'] = profile.get_passwd()
    host_config['name']   = profile.get_name()  
    host_config['home']   = profile.get_home()
    host_config['comm']   = profile.get_comm()
    host_config['download'] = profile.get_download()
    host_config['duser'] = profile.get_duser()
    host_config['dpasswd'] = profile.get_dpasswd()
    host_config['ftpport'] = profile.get_ftpport()
    
    if host_config['type'] == 'S_DMGR' or host_config['type'] == 'S_NODE' or host_config['type'] == 'S_SERVER':
        
        host_config['web_admin_user'] = profile.get_web_admin_user()       
        host_config['web_admin_pass'] = profile.get_web_admin_pass()
    
    if host_config['type'] == 'SERVER' or host_config['type'] == 'S_SERVER':
        
        host_config['server'] = profile.get_server()
    
    elif host_config['type'] == 'S_DMGR' or host_config['type'] == 'S_NODE' or host_config['type'] == 'S_SERVER':
        
        host_config['web_admin_user'] = profile.get_web_admin_user()       
        host_config['web_admin_pass'] = profile.get_web_admin_pass()
        
    
    if action_type == 'START':
        
        if  host_config['comm'] == 'TELNET':
            
            command_list =  []
            
            checkpoint = 'ADMU3000I'
            
            CommandMaker.make_start_command(host_config, command_list)
            
            ConnFactory.conn_tel(host_config, action_type, command_list, checkpoint)
        
        else:
            
            print 'The communicate way: ' + host_config['comm'] + ' is unknown.'
    
    elif action_type == 'STOP':
        
        if  host_config['comm'] == 'TELNET':
            
            command_list =  []
            
            checkpoint = 'ADMU4000I'
            
            CommandMaker.make_stop_command(host_config, command_list)
            
            msg = ConnFactory.conn_tel(host_config, action_type, command_list, checkpoint)
            log(msg)
        else:
            
            print 'The communicate way: ' + host_config['comm'] + ' is unknown.'
    
    elif action_type == 'GLOG':
        
        if host_config['download'] == 'FTP':
            
            command_list = []
            
            #CommandMaker.make_ftp_command(host_config, command_list)
            ConnFactory.conn_ftp(host_config, action_type, command_list)
        
        else:
            
            print 'The download way: ' + host_config['download'] + ' is unknown.'

    elif action_type == 'DLOG':
        
        if  host_config['comm'] == 'TELNET':
            
            command_list =  []
            
            checkpoint = ''
                     
            CommandMaker.make_command(host_config, action_type, command_list)
            
            ConnFactory.conn_tel(host_config, action_type, command_list, checkpoint)
        
        else:
            
            print 'The communicate way: ' + host_config['comm'] + ' is unknown.'
    
    elif action_type == 'DTEMP':
        
        if  host_config['comm'] == 'TELNET':
            
            command_list =  []
            
            checkpoint = ''
                     
            CommandMaker.make_command(host_config, action_type, command_list)
            
            ConnFactory.conn_tel(host_config, action_type, command_list, checkpoint)
        
        else:
            
            print 'The communicate way: ' + host_config['comm'] + ' is unknown.'
        
    
    else:
        
        print 'the action type: ' + action_type + ' is unknown.'

def apache_action(profile, action_type):
    
    host_config = {}
    host_config['id'] = profile.get_id()
    host_config['section']= profile.get_section()
    host_config['host']   = profile.get_host()
    host_config['system'] = profile.get_system()
    host_config['user']   = profile.get_user()
    host_config['passwd'] = profile.get_passwd()
    host_config['home']   = profile.get_home()
    host_config['comm']   = profile.get_comm()
    host_config['download'] = profile.get_download()
    host_config['duser'] = profile.get_duser()
    host_config['dpasswd'] = profile.get_dpasswd()
    host_config['ftpport'] = profile.get_ftpport()
    
    if action_type == 'START':
        
        if  host_config['comm'] == 'TELNET':
            
            command_list =  []
            
            checkpoint = '-k start'
            
            CommandMaker.make_start_command(host_config, command_list)
            
            msg = ConnFactory.conn_tel(host_config, action_type, command_list, checkpoint)
            
            log(msg)
            
        else:
            
            print 'The communicate way: ' + host_config['comm'] + ' is unknown.'
    
    elif action_type == 'STOP':
        
        if  host_config['comm'] == 'TELNET':
            
            command_list =  []
            
            checkpoint = 'grep httpd'
            
            CommandMaker.make_stop_command(host_config, command_list)
            
            msg = ConnFactory.conn_tel(host_config, action_type, command_list, checkpoint)
            
            log(msg)
            
        else:
            
            print 'The communicate way: ' + host_config['comm'] + ' is unknown.'
    
    elif action_type == 'GLOG':
        
        if host_config['download'] == 'FTP':
            
            command_list = []
            
            ConnFactory.conn_ftp(host_config, action_type, command_list)
        
        else:
            
            print 'The download way: ' + host_config['download'] + ' is unknown.'
    else:
        
        print 'the action type: ' + action_type + ' is unknown.'

def db2_action(profile, action_type):

    host_config = {}
    host_config['id'] = profile.get_id()
    host_config['section']= profile.get_section()
    host_config['host']   = profile.get_host()
    host_config['system'] = profile.get_system()
    host_config['user']   = profile.get_user()
    host_config['passwd'] = profile.get_passwd()
    host_config['comm']   = profile.get_comm()
    
    if action_type == 'START':
        
        if  host_config['comm'] == 'TELNET':
            
            command_list =  []
            
            checkpoint = 'SQL1063N'
            
            CommandMaker.make_start_command(host_config, command_list)
            
            ConnFactory.conn_tel(host_config, action_type, command_list, checkpoint)
        
        else:
            
            print 'The communicate way: ' + host_config['comm'] + ' is unknown.'
    
    elif action_type == 'STOP':
        
        if  host_config['comm'] == 'TELNET':
            
            command_list =  []
            
            checkpoint = 'SQL1064N'
            
            CommandMaker.make_stop_command(host_config, command_list)
            
            ConnFactory.conn_tel(host_config, action_type, command_list, checkpoint)
        
        else:
            
            print 'The communicate way: ' + host_config['comm'] + ' is unknown.'
    
    elif action_type == 'GLOG':
        
        if host_config['download'] == 'FTP':
            
            command_list = []
            
            ConnFactory.conn_ftp(host_config, action_type, command_list)
        
        else:
            
            print 'The download way: ' + host_config['download'] + ' is unknown.'
    else:
        
        print 'the action type: ' + action_type + ' is unknown.'    
    

def aix_action(profile, action_type):
    
    host_config = {}
    host_config['id'] = profile.get_id()
    host_config['section']= profile.get_section()
    host_config['host']   = profile.get_host()
    host_config['system']   = profile.get_system()
    host_config['user']   = profile.get_user()
    host_config['passwd'] = profile.get_passwd()
    host_config['comm']   = profile.get_comm()
    host_config['download'] = profile.get_download()
    host_config['duser'] = profile.get_duser()
    host_config['dpasswd'] = profile.get_dpasswd()
    host_config['ftpport'] = profile.get_ftpport()
    
    if action_type == 'GCONF':
        
        if  host_config['comm'] == 'TELNET':
            
            command_list =  []
            
            checkpoint = ''
            
            CommandMaker.make_command(host_config, action_type, command_list)
            
            conf = ConnFactory.conn_tel(host_config, action_type, command_list, checkpoint)
            
            print conf
        
        else:
            
            print 'The communicate way: ' + host_config['comm'] + ' is unknown.'
            
        #if host_config['download'] == 'FTP':
        #
        #    command_list = []
        #    
        #    ConnFactory.conn_ftp(host_config, action_type, command_list)
        #
        #else:
        #    
        #    print 'The download way: ' + host_config['download'] + ' is unknown.'            
            
    
    else:
        
        print 'the action type: ' + action_type + ' is unknown.'

def suit_action(profile):
    
    profile_array = profile.get_profiles()
    action_array = profile.get_actions()
    
    i = 0
    for profile in profile_array:
        
        #check the profile is exist or not
        if validator.check_profile(profile):
        
            action(profile, string.upper(action_array[i]))
           
        else:
        
            print 'the profile: ' + profile_name + ' is not exist. '
        
        i = i + 1
    
